package com.module.admin.sys.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.module.admin.sys.enums.SysMenuType;
import com.module.admin.sys.pojo.SysMenu;
import com.module.admin.sys.service.SysMenuService;
import com.system.comm.utils.FrameStringUtil;

/**
 * 系统菜单
 * @author yuejing
 * @date 2016年5月5日 下午12:22:58
 * @version V1.0.0
 */
@Component
public class SysMenuServiceImpl implements SysMenuService {
	/**
	 * 菜单
	 */
	private static Map<String, SysMenu> menuMap = new HashMap<String, SysMenu>();
	private static List<SysMenu> treeList = new ArrayList<SysMenu>();

	static {
		SysMenu menu = addParent("workbench", SysMenuType.MENU, "工作台", "glyphicon glyphicon-home", "/sysUser/f-view/main", 1);
		menu.addCldMenu(addCld("workbenchAppSum", "workbench", SysMenuType.FUNCTION, "应用监控", "", null, 10));
		menu.addCldMenu(addCld("workbenchTtsProject", "workbench", SysMenuType.FUNCTION, "定时任务监控", "", null, 11));
		menu.addCldMenu(addCld("workbenchAppInfo", "workbench", SysMenuType.FUNCTION, "应用列表", "", null, 20));
		menu.addCldMenu(addCld("workbenchPrjInfo", "workbench", SysMenuType.FUNCTION, "服务列表", "", null, 30));
		
		menu = addParent("app", SysMenuType.MENU, "应用管理", "glyphicon glyphicon-folder-open", null, 2);
		menu.addCldMenu(addCld("appInfoManager", "prj", SysMenuType.MENU, "应用列表", "", "/appInfo/f-view/manager", 1));
		menu.addCldMenu(addCld("appInfoQueryAll", "appInfoManager", SysMenuType.FUNCTION, "应用-查看所有", "", null, 10));
		menu.addCldMenu(addCld("appInfoManagerAdd", "appInfoManager", SysMenuType.FUNCTION, "应用-新增", "", null, 11));
		menu.addCldMenu(addCld("appInfoManagerUpd", "appInfoManager", SysMenuType.FUNCTION, "应用-修改", "", null, 12));
		menu.addCldMenu(addCld("appInfoManagerDel", "appInfoManager", SysMenuType.FUNCTION, "应用-删除", "", null, 13));
		menu.addCldMenu(addCld("appInfoManagerApiAdd", "appInfoManager", SysMenuType.FUNCTION, "应用-API自动化-新增", "", null, 30));
		menu.addCldMenu(addCld("appInfoManagerApiUpd", "appInfoManager", SysMenuType.FUNCTION, "应用-API自动化-修改", "", null, 31));
		menu.addCldMenu(addCld("appInfoManagerApiDel", "appInfoManager", SysMenuType.FUNCTION, "应用-API自动化-删除", "", null, 32));
		menu.addCldMenu(addCld("appInfoManagerApiDtlAdd", "appInfoManager", SysMenuType.FUNCTION, "应用-API自动化-接口-新增", "", null, 33));
		menu.addCldMenu(addCld("appInfoManagerApiDtlUpd", "appInfoManager", SysMenuType.FUNCTION, "应用-API自动化-接口-修改", "", null, 34));
		menu.addCldMenu(addCld("appInfoManagerApiDtlDel", "appInfoManager", SysMenuType.FUNCTION, "应用-API自动化-接口-删除", "", null, 35));
		
		menu = addParent("prj", SysMenuType.MENU, "服务管理", "glyphicon glyphicon-folder-close", null, 3);
		menu.addCldMenu(addCld("prjInfoManager", "prj", SysMenuType.MENU, "服务列表", "", "/prjInfo/f-view/manager", 1));
		menu.addCldMenu(addCld("prjInfoManagerAdd", "prjInfoManager", SysMenuType.FUNCTION, "服务-新增", "", null, 10));
		menu.addCldMenu(addCld("prjInfoManagerUpd", "prjInfoManager", SysMenuType.FUNCTION, "服务-修改", "", null, 11));
		menu.addCldMenu(addCld("prjInfoManagerDel", "prjInfoManager", SysMenuType.FUNCTION, "服务-删除", "", null, 12));
		menu.addCldMenu(addCld("prjInfoManagerConfigUpd", "prjInfoManager", SysMenuType.FUNCTION, "服务-配置文件-修改", "", null, 20));
		menu.addCldMenu(addCld("prjVersionDeployment", "prjInfoManager", SysMenuType.FUNCTION, "服务-部署版本", "", null, 50));
		menu.addCldMenu(addCld("prjVersionStop", "prjInfoManager", SysMenuType.FUNCTION, "服务-部署版本-停止服务", "", null, 51));
		menu.addCldMenu(addCld("prjVersionRestart", "prjInfoManager", SysMenuType.FUNCTION, "服务-部署版本-重启服务", "", null, 52));

		menu = addParent("tts", SysMenuType.MENU, "定时任务", "glyphicon glyphicon-time", null, 5);
		menu.addCldMenu(addCld("servInfoManager", "tts", SysMenuType.MENU, "服务管理", "", "/ttsServInfo/f-view/manager", 10));
		menu.addCldMenu(addCld("projectManager", "tts", SysMenuType.MENU, "项目管理", "", "/ttsTaskProject/f-view/manager", 20));
		menu.addCldMenu(addCld("projectManagerAdd", "projectManager", SysMenuType.FUNCTION, "项目-新增", "", null, 21));
		menu.addCldMenu(addCld("projectManagerUpd", "projectManager", SysMenuType.FUNCTION, "项目-修改", "", null, 22));
		menu.addCldMenu(addCld("projectManagerDel", "projectManager", SysMenuType.FUNCTION, "项目-删除", "", null, 23));
		menu.addCldMenu(addCld("projectManagerJobAdd", "projectManager", SysMenuType.FUNCTION, "Job-新增", "", null, 24));
		menu.addCldMenu(addCld("projectManagerJobUpd", "projectManager", SysMenuType.FUNCTION, "Job-修改", "", null, 25));
		menu.addCldMenu(addCld("projectManagerJobDel", "projectManager", SysMenuType.FUNCTION, "Job-删除", "", null, 26));
		menu.addCldMenu(addCld("projectManagerJobExec", "projectManager", SysMenuType.FUNCTION, "Job-立即执行", "", null, 27));
		
		menu.addCldMenu(addCld("projectChart", "tts", SysMenuType.MENU, "项目图表", "", "/ttsTaskProject/f-view/chart", 30));
		menu.addCldMenu(addCld("projectJobLog", "tts", SysMenuType.MENU, "异常调度", "", "/ttsTaskJobLog/f-view/manager", 40));

		menu = addParent("cli", SysMenuType.MENU, "Agent", "glyphicon glyphicon-eur", null, 6);
		menu.addCldMenu(addCld("cliInfoManager", "cli", SysMenuType.MENU, "Agent管理", "", "/cliInfo/f-view/manager", 1));
		menu.addCldMenu(addCld("cliInfoMonitor", "cli", SysMenuType.MENU, "Agent监控", "", "/cliInfo/f-view/monitor", 2));
		
		menu = addParent("sys", SysMenuType.MENU, "系统管理", "glyphicon glyphicon-cog", null, 7);
		menu.addCldMenu(addCld("sysRoleManager", "sys", SysMenuType.MENU, "角色管理", "", "/sysRole/f-view/manager", 1));
		menu.addCldMenu(addCld("sysUserManager", "sys", SysMenuType.MENU, "用户管理", "", "/sysUser/f-view/manager", 2));
		menu.addCldMenu(addCld("msSecretManager", "sys", SysMenuType.MENU, "秘钥管理", "", "/msSecret/f-view/manager", 3));
		menu.addCldMenu(addCld("msConfigManager", "sys", SysMenuType.MENU, "微服务配置", "", "/msConfig/f-view/manager", 4));
		menu.addCldMenu(addCld("sysConfigManager", "sys", SysMenuType.MENU, "参数配置", "", "/sysConfig/f-view/manager", 5));
	}
	public static Map<String, SysMenu> getMapMenus() {
		return menuMap;
	}

	/**
	 * 根据所属类型获取树形集合
	 * @param network
	 * @return
	 */
	public static List<SysMenu> getTreeMenus() {
		return treeList;
	}
	
	/**
	 * 获取所有菜单  tree
	 * @return
	 */
	public List<SysMenu> findAllTreeMenu(){
		return getTreeMenus();
	}

	/**
	 * 根据所属类型获取集合
	 * @param network
	 * @return
	 */
	public static List<SysMenu> getMenus(Integer network) {
		List<SysMenu> list = new ArrayList<SysMenu>();
		for (SysMenu m : treeList) {
			list.add(new SysMenu(m));
			for (SysMenu cld : m.getCldMenus()) {
				list.add(new SysMenu(cld));
			}
		}
		return list;
	}

	/**
	 * 添加父级菜单
	 * @param menuId
	 * @param sysMenuType
	 * @param name
	 * @param icon
	 * @param url
	 * @param isshow
	 * @return
	 */
	public static SysMenu addParent(String menuId, SysMenuType sysMenuType, String name, String icon, String url, Integer isshow) {
		SysMenu menu = new SysMenu();
		menu.setMenuId(menuId);
		menu.setPid("0");
		menu.setType(sysMenuType.getCode());
		menu.setName(name);
		menu.setIcon(icon);
		menu.setUrl(url);
		menu.setIsshow(isshow);
		menuMap.put(menuId, menu);
		treeList.add(menu);
		return menu;
	}
	/**
	 * 添加子级菜单
	 * @param menuId
	 * @param pid
	 * @param sysMenuType
	 * @param name
	 * @param icon
	 * @param url
	 * @param isshow
	 * @return
	 */
	private static SysMenu addCld(String menuId, String pid, SysMenuType sysMenuType, String name, String icon, String url, Integer isshow) {
		SysMenu menu = new SysMenu();
		menu.setMenuId(menuId);
		menu.setPid(pid);
		menu.setType(sysMenuType.getCode());
		menu.setName(name);
		menu.setIcon(icon);
		menu.setUrl(url);
		menu.setIsshow(isshow);
		menuMap.put(menuId, menu);
		return menu;
	}

	/**
	 * 根据菜单ID字符串集合获取菜单集合[,分隔]
	 * @param ids
	 * @return
	 */
	@Override
	public List<SysMenu> findByMenuIds(String menuIds) {
		List<SysMenu> list = new ArrayList<SysMenu>();
		List<String> idArr = FrameStringUtil.toArray(menuIds, ",");
		for (String id : idArr) {
			SysMenu menu = menuMap.get(id);
			if(menu != null) {
				list.add(new SysMenu(menu));
			}
		}
		return list;
	}

	@Override
	public List<SysMenu> findTreeByMenuIds(String menuIds) {
		List<SysMenu> treeList = new ArrayList<SysMenu>();
		if(FrameStringUtil.isEmpty(menuIds)) {
			return treeList;
		}
		List<SysMenu> list = getTreeMenus();
		for (SysMenu sysMenu : list) {
			if(menuIds.contains(sysMenu.getMenuId())) {
				SysMenu menu = new SysMenu(sysMenu);
				if(sysMenu.getCldMenus() != null) {
					for (SysMenu cld : sysMenu.getCldMenus()) {
						if(menuIds.contains(cld.getMenuId())) {
							SysMenu cldMenu = new SysMenu(cld);
							menu.addCldMenu(cldMenu);
							if(cld.getCldMenus() != null) {
								for (SysMenu cld2 : cld.getCldMenus()) {
									if(menuIds.contains(cld2.getMenuId().toString())) {
										cldMenu.addCldMenu(new SysMenu(cld2));
									}
								}
							}
						}
					}
				}
				treeList.add(menu);
			}
		}
		return treeList;
	}
}